package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.componetcotainer;

import com.hannesdorfmann.mosby3.mvi.integrationtest.ResourceTable;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.componetcotainer.TestMviStackLayout;
import com.hannesdorfmann.mosby3.mvi.integrationtest.slice.MviComponentContainerAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;

public class MviComponentContainerAbility extends Ability {

    private static volatile MviComponentContainerAbility currentInstance;
    TestMviStackLayout testFrameLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        currentInstance = getInstance();
        super.setUIContent(ResourceTable.Layout_ability_componentcontainer_mvi);
        testFrameLayout = (TestMviStackLayout) findComponentById(ResourceTable.Id_testFrameLayout);
        testFrameLayout.setmComponetContext(this);
//        super.setMainRoute(MviComponentContainerAbilitySlice.class.getName());

    }

    public static MviComponentContainerAbility getInstance(){
        if(MviComponentContainerAbility.currentInstance == null){
            MviComponentContainerAbility.currentInstance = new MviComponentContainerAbility();
        }
        return  MviComponentContainerAbility.currentInstance;
    }

    public static TestMviStackLayout getMviViewGroup() {
        return (TestMviStackLayout) currentInstance.findComponentById(ResourceTable.Id_testFrameLayout);
    }

    public static void pressBackButton() {
        currentInstance.getUITaskDispatcher().syncDispatch(new Runnable() {
            @Override public void run() {
                currentInstance.onBackPressed();
            }
        });
    }

    public static void removeMviViewGroup() {
        currentInstance.getUITaskDispatcher().syncDispatch(new Runnable() {
            @Override public void run() {
                ComponentContainer rootView = (ComponentContainer) currentInstance.findComponentById(ResourceTable.Id_rootView);
                rootView.removeAllComponents();
            }
        });
    }
}
